package cn.iocoder.yudao.module.demo.service.student;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.demo.controller.admin.student.vo.StudentPageReqVO;
import cn.iocoder.yudao.module.demo.controller.admin.student.vo.StudentSaveReqVO;
import cn.iocoder.yudao.module.demo.dal.dataobject.student.StudentClassDO;
import cn.iocoder.yudao.module.demo.dal.dataobject.student.StudentCourseDO;
import cn.iocoder.yudao.module.demo.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.demo.dal.mysql.student.StudentClassMapper;
import cn.iocoder.yudao.module.demo.dal.mysql.student.StudentCourseMapper;
import cn.iocoder.yudao.module.demo.dal.mysql.student.StudentMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.demo.enums.ErrorCodeConstants.*;

/**
 * 学生 Service 实现类
 *
 * @author leesea
 */
@Service
@Validated
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;
    @Resource
    private StudentClassMapper studentClassMapper;
    @Resource
    private StudentCourseMapper studentCourseMapper;

    @Override
    public Long createStudent(StudentSaveReqVO createReqVO) {
        // 插入
        StudentDO student = BeanUtils.toBean(createReqVO, StudentDO.class);
        studentMapper.insert(student);
        // 返回
        return student.getId();
    }

    @Override
    public void updateStudent(StudentSaveReqVO updateReqVO) {
        // 校验存在
        validateStudentExists(updateReqVO.getId());
        // 更新
        StudentDO updateObj = BeanUtils.toBean(updateReqVO, StudentDO.class);
        studentMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStudent(Long id) {
        // 校验存在
        validateStudentExists(id);
        // 删除
        studentMapper.deleteById(id);

        // 删除子表
        deleteStudentClassByStudentId(id);
        deleteStudentCourseByStudentId(id);
    }

    private void validateStudentExists(Long id) {
        if (studentMapper.selectById(id) == null) {
            throw exception(STUDENT_NOT_EXISTS);
        }
    }

    @Override
    public StudentDO getStudent(Long id) {
        return studentMapper.selectById(id);
    }

    @Override
    public PageResult<StudentDO> getStudentPage(StudentPageReqVO pageReqVO) {
        return studentMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（学生班级） ====================

    @Override
    public PageResult<StudentClassDO> getStudentClassPage(PageParam pageReqVO, Long studentId) {
        return studentClassMapper.selectPage(pageReqVO, studentId);
    }

    @Override
    public Long createStudentClass(StudentClassDO studentClass) {
        // 校验是否已经存在
        if (studentClassMapper.selectByStudentId(studentClass.getStudentId()) != null) {
            throw exception(STUDENT_CLASS_EXISTS);
        }
        // 插入
        studentClassMapper.insert(studentClass);
        return studentClass.getId();
    }

    @Override
    public void updateStudentClass(StudentClassDO studentClass) {
        // 校验存在
        validateStudentClassExists(studentClass.getId());
        // 更新(解决更新情况下：updateTime 不更新)
        studentClass.setUpdater(null).setUpdateTime(null);
        studentClassMapper.updateById(studentClass);
    }

    @Override
    public void deleteStudentClass(Long id) {
        // 校验存在
        validateStudentClassExists(id);
        // 删除
        studentClassMapper.deleteById(id);
    }

    @Override
    public StudentClassDO getStudentClass(Long id) {
        return studentClassMapper.selectById(id);
    }

    private void validateStudentClassExists(Long id) {
        if (studentClassMapper.selectById(id) == null) {
            throw exception(STUDENT_CLASS_NOT_EXISTS);
        }
    }

    private void deleteStudentClassByStudentId(Long studentId) {
        studentClassMapper.deleteByStudentId(studentId);
    }

    // ==================== 子表（学生课程） ====================

    @Override
    public PageResult<StudentCourseDO> getStudentCoursePage(PageParam pageReqVO, Long studentId) {
        return studentCourseMapper.selectPage(pageReqVO, studentId);
    }

    @Override
    public Long createStudentCourse(StudentCourseDO studentCourse) {
        studentCourseMapper.insert(studentCourse);
        return studentCourse.getId();
    }

    @Override
    public void updateStudentCourse(StudentCourseDO studentCourse) {
        // 校验存在
        validateStudentCourseExists(studentCourse.getId());
        // 更新(解决更新情况下：updateTime 不更新)
        studentCourse.setUpdater(null).setUpdateTime(null);
        studentCourseMapper.updateById(studentCourse);
    }

    @Override
    public void deleteStudentCourse(Long id) {
        // 校验存在
        validateStudentCourseExists(id);
        // 删除
        studentCourseMapper.deleteById(id);
    }

    @Override
    public StudentCourseDO getStudentCourse(Long id) {
        return studentCourseMapper.selectById(id);
    }

    private void validateStudentCourseExists(Long id) {
        if (studentCourseMapper.selectById(id) == null) {
            throw exception(STUDENT_COURSE_NOT_EXISTS);
        }
    }

    private void deleteStudentCourseByStudentId(Long studentId) {
        studentCourseMapper.deleteByStudentId(studentId);
    }

}
